<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\ElectricAccountingRecords;
use App\Models\ElectricPlatformAgent;
use Illuminate\Http\Request;

class ElectricAccountingRecordsController extends Controller
{
    public function index(Request $request)
    {
        $limit = $request->get('limit', 20);

        $agent_mobile = $request->get('agent_mobile');

        $model = ElectricAccountingRecords::query()->with('electric_platform_agent:id,agent_name,agent_mobile')->orderByDesc('id');

        if ($agent_mobile) {
            $ids = ElectricPlatformAgent::query()->where('agent_mobile', $agent_mobile)->pluck('id');
            $model = $model->whereIn('agent_id', $ids);
        }

        $agent_name = $request->get('agent_name');

        if ($agent_name) {
            $ids = ElectricPlatformAgent::query()->where('agent_name', $agent_name)->pluck('id');
            $model = $model->whereIn('agent_id', $ids);
        }

        $data = $model->paginate($limit);
        return $this->success($data);
    }

    public function status(Request $request)
    {
        $data = $request->post();

        $id = $data['id'];

        $res = ElectricAccountingRecords::query()->where('id', $id)->update($data);

        return $res ? $this->success() : $this->error();
    }
}
